package com.gms.controller.eventmanager;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gms.entity.event.Event;
import com.gms.entity.event.Referee;
import com.gms.entity.user.User;
import com.gms.service.eventmanager.EventService;
import com.gms.service.eventmanager.RefereeService;
import com.gms.service.placemanager.PlaceApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/event")
public class EventManagerController {
    @Autowired
    EventService eventService;
    @Autowired
    RefereeService refereeService;
    @Autowired
    PlaceApplyService placeApplyService;

    @RequestMapping("/event_status")
    public String event_status(Model model, @RequestParam("status") int status){
        String Event_status = null;
        switch (status){
            case 1 :
                Event_status = "待审核";
                break;
            case 2 :
                Event_status = "审核通过";
                break;
            case 3 :
                Event_status = "审核未通过";
                break;
        }
        Page<Event> page = eventService.queryEvent_status(1,Event_status);
        model.addAttribute("page",page);
        model.addAttribute("Event_status",Event_status);
        return "event/audit";
    }

    @RequestMapping("/pagejump")
    public String page_jump(@Nullable Integer pageNum,@Nullable String Event_status,Model model){
        Page<Event> page = eventService.queryEvent_status(pageNum,Event_status);
        model.addAttribute("page",page);
        model.addAttribute("Event_status",Event_status);
        return "event/audit";
    }

    @RequestMapping("/eventInformation")
    public String eventInformation(@Nullable String EventId,Model model){
        Event event = eventService.queryEvent_id(EventId);
        model.addAttribute("event",event);
        return "event/eventInformation";
    }

    @PostMapping("/audit")
    public String audit(@Nullable String EventId,String AuditorText,String EventStatus, HttpSession session,Model model){
        Event event = new Event();
        event.setEventId(EventId);
        event.setEventStatus(EventStatus);
        User user = (User) session.getAttribute("user");
        event.setAuditor(user.getUserName());
        event.setAuditorText(AuditorText);
        eventService.updateAuditor(event);
        if(EventStatus.equals("审核未通过")){
            placeApplyService.UpdatePlaceApplyState(null,EventId,null,"已退订");
        }
        Page<Event> page = eventService.queryEvent_status(1,EventStatus);
        model.addAttribute("page",page);
        model.addAttribute("Event_status",EventStatus);
        return "event/audit";
    }

    @RequestMapping("/event_process")
    public String event_process(@Nullable Integer pageNum,@Nullable Integer status,Model model){
        Page<Event> page = new Page<>();
        switch (status){
            case 1 :
                page = eventService.queryEvent_before(pageNum);
                break;
            case 2 :
                page = eventService.queryEvent_in(pageNum);
                break;
            case 3 :
                page = eventService.queryEvent_end(pageNum);
                break;
        }
        model.addAttribute("page",page);
        model.addAttribute("status",status);
        return "event/eventProcess";
    }

    @RequestMapping("/eventInformation1")
    public String eventInformation1(@Nullable String EventId,Model model){
        Event event = eventService.queryEvent_id(EventId);
        model.addAttribute("event",event);
        return "event/eventInformation1";
    }

    @PostMapping("/event_query")
    public String event_query(@Nullable Integer pageNum,@Nullable Integer status,
                              String EventId,String EventName,String EventStartTime,
                              String EventEndTime,Model model){
        Page<Event> page = new Page<>();
        switch (status){
            case 1 :
                page = eventService.queryAll(pageNum);
                break;
            case 2 :
                SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
                Date StartTime = null;
                Date EndTime = null;
                try {
                    StartTime = dateformat.parse(EventStartTime);
                    EndTime = dateformat.parse(EventEndTime);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                page = eventService.queryEvent(pageNum,EventId,EventName,StartTime,EndTime);
                break;
        }
        model.addAttribute("page",page);
        model.addAttribute("status",status);
        model.addAttribute("EventId",EventId);
        model.addAttribute("EventName",EventName);
        model.addAttribute("EventStartTime",EventStartTime);
        model.addAttribute("EventEndTime",EventEndTime);
        return "event/eventQuery";
    }

    @RequestMapping("/event_query_jump")
    public String event_query_jump(@Nullable Integer pageNum,@Nullable Integer status, @Nullable String EventId,
                                   @Nullable String EventName,@Nullable String EventStartTime, @Nullable String EventEndTime,
                                   Model model){
        Page<Event> page = new Page<>();
        switch (status){
            case 1 :
                page = eventService.queryAll(pageNum);
                break;
            case 2 :
                SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
                Date StartTime = null;
                Date EndTime = null;
                try {
                    StartTime = dateformat.parse(EventStartTime);
                    EndTime = dateformat.parse(EventEndTime);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                page = eventService.queryEvent(pageNum,EventId,EventName,StartTime,EndTime);
                break;
        }
        model.addAttribute("page",page);
        model.addAttribute("status",status);
        model.addAttribute("EventId",EventId);
        model.addAttribute("EventName",EventName);
        model.addAttribute("EventStartTime",EventStartTime);
        model.addAttribute("EventEndTime",EventEndTime);
        return "event/eventQuery";
    }

    @RequestMapping("/event_cancel")
    public String event_query(@Nullable Integer pageNum,String EventId,Model model){
        Page<Event> page = eventService.queryEvent_id_status(pageNum,EventId,"审核通过");
        model.addAttribute("page",page);
        return "event/eventcancel";
    }

    @RequestMapping("/eventcancel")
    public String eventcancel(@Nullable String EventId,Model model){
        Event event = new Event();
        event.setEventId(EventId);
        event.setEventStatus("赛事取消");
        eventService.updateAuditor(event);
        Page<Event> page = eventService.queryEvent_id_status(1,null,"审核通过");
        placeApplyService.UpdatePlaceApplyState(null,EventId,null,"已退订");
        model.addAttribute("page",page);
        return "event/eventcancel";
    }

    @RequestMapping("/touserevent")
    public String touserevent(@Nullable Integer pageNum,@Nullable String userId,Model model){
        Page<Event> page = eventService.queryEvent_user(pageNum,userId);
        model.addAttribute("page",page);
        return "event/eventuser";
    }

    @RequestMapping("/toeventcreate")
    public String toeventcreate(Model model){
        List<Referee> list = refereeService.queryAll();
        model.addAttribute("list",list);
        return "event/eventcreate";
    }

    @PostMapping("/eventcreate")
    public String eventcreate(Event event,@Nullable String Applicants,String EventStartTime1,String EventEndTime1){
        event.setEventId("00000000");
        event.setRefereeName(refereeService.queryName(event.getRefereeId()));
        event.setApplicants(Applicants);
        event.setEventStatus("待审核");
        SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
        Date StartTime = null;
        Date EndTime = null;
        try {
            StartTime = dateformat.parse(EventStartTime1);
            EndTime = dateformat.parse(EventEndTime1);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        event.setEventStartTime(StartTime);
        event.setEventEndTime(EndTime);
        eventService.insertEvent(event);
        placeApplyService.UpdateApply("00000000",eventService.queryEventId());
        return "redirect:/user/index";
    }

    @RequestMapping("/toeventupdate")
    public String toeventupdate(@Nullable String EventId,Model model){
        Event event = eventService.queryEvent_id(EventId);
        List<Referee> list = refereeService.queryAll();
        model.addAttribute("list",list);
        model.addAttribute("event",event);
        return "event/eventupdate";
    }

    @PostMapping("/eventupdate")
    public String eventupdate(@Nullable String EventId,Event event,Model model,
                              @Nullable String userId){
        event.setEventId(EventId);
        event.setEventStatus("待审核");
        eventService.updateAuditor(event);
        Page<Event> page = eventService.queryEvent_user(1,userId);
        model.addAttribute("page",page);
        return "event/eventuser";
    }

    @RequestMapping("/toeventNotice")
    public String toeventNotice(@Nullable Integer pageNum,@Nullable Integer status,Model model){
        Page<Event> page = new Page<>();
        switch (status){
            case 1 :
                page = eventService.queryEvent_before(pageNum);
                break;
            case 2 :
                page = eventService.queryEvent_in(pageNum);
                break;
            case 3 :
                page = eventService.queryEventNoticeAll(pageNum);
                break;
        }
        model.addAttribute("page",page);
        model.addAttribute("status",status);
        return "event/eventNotice";
    }

    @RequestMapping("/insertNotice")
    public String insertNotice(@Nullable String EventId,@Nullable Integer pageNum,@Nullable Integer status,Model model){
        eventService.insertNotice(EventId);
        Page<Event> page = new Page<>();
        switch (status){
            case 1 :
                page = eventService.queryEvent_before(pageNum);
                break;
            case 2 :
                page = eventService.queryEvent_in(pageNum);
                break;
            case 3 :
                page = eventService.queryEventNoticeAll(pageNum);
                break;
        }
        model.addAttribute("page",page);
        model.addAttribute("status",status);
        return "event/eventNotice";
    }

    @RequestMapping("/deleteNotice")
    public String deleteNotice(@Nullable String EventId,@Nullable Integer pageNum,@Nullable Integer status,Model model){
        eventService.deleteNotice(EventId);
        Page<Event> page = new Page<>();
        switch (status){
            case 1 :
                page = eventService.queryEvent_before(pageNum);
                break;
            case 2 :
                page = eventService.queryEvent_in(pageNum);
                break;
            case 3 :
                page = eventService.queryEventNoticeAll(pageNum);
                break;
        }
        model.addAttribute("page",page);
        model.addAttribute("status",status);
        return "event/eventNotice";
    }
}
